package org.plex.core.imp;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.plex.core.Authentication;

import com.adobe.idp.Context;
import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;
import com.adobe.idp.um.api.UMException;
import com.adobe.idp.um.api.infomodel.AuthResult;
import com.adobe.livecycle.usermanager.client.AuthenticationManagerServiceClient;

public class AuthenticationImp implements Authentication
{

	private ServiceClientFactory	serviceClientFactory;
	private AuthResult				authResult;
	private Context					context;

	public AuthenticationImp()
	{
		PropertiesImp imp = new PropertiesImp();
		serviceClientFactory = ServiceClientFactory.createInstance(imp.getProperties());
	}

	public org.plex.core.Context getContext()
	{
		if (context == null)
		{
			System.out.println("context is null");
		}
		return context == null ? null : new ContextImp(context);
	}

	public boolean doLogin(String userid, String password)
	{
		AuthenticationManagerServiceClient userManagerUtilServiceClient = new AuthenticationManagerServiceClient(
				serviceClientFactory);
		try
		{
			Map map = new HashMap();
			map.put("AUDIT_ENABLED", Boolean.FALSE);
			authResult = userManagerUtilServiceClient.authenticate(userid,
					password.getBytes("utf-8"));
			context = new Context();
			context.initPrincipal(authResult);
		} catch (UMException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (UnsupportedEncodingException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean doLogout()
	{

		return true;
	}

}
